from django.conf import settings
from django.shortcuts import render

# Create your views here.
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView, CreateAPIView, RetrieveAPIView, UpdateAPIView, ListAPIView
from itsdangerous import TimedJSONWebSignatureSerializer as TJS
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from users import serializers
from users.models import User, Area, Address
from users.serializers import UserSerializers, UserDetailSerializer, UserEmailSerializer, AreaSerializer, \
    AddressSerializer


#验证用户名是否存在
class UserNameCountView(CreateAPIView):
    def get(self,request,username):

        count = User.objects.filter(username=username).count()

        return Response(
            {
                'count':count
            }
        )

#验证手机号是否存在
class MobileCountView(APIView):
    def get(self, request, mobile):
        count = User.objects.filter(mobile=mobile).count()

        return Response(
            {
                'count': count
            }
        )
#注册
class UserView(CreateAPIView):

    serializer_class = serializers.UserSerializers

#返回用户个人中心数据
class UserDetailView(RetrieveAPIView):

    serializer_class =  UserDetailSerializer

    def get_object(self):

        return self.request.user

#保存邮箱
class UserEmailView(UpdateAPIView):

    serializer_class = UserEmailSerializer


    def get_object(self):
        #重写方法
        return self.request.user


class EmailVirifyView(APIView):

    def get(self,request):
        #获取前段数据
        data = request.query_params.get('token')

        if not data:
            return Response({'message':'token参数缺失'},status=400)
        #解密
        tjs = TJS(settings.SECRET_KEY, 300)
        try:
            data = tjs.loads(data)

        except:
            return Response({'message':'token参数无效'},status=400)

        #查询用户
        username = data.get('username')

        user = User.objects.get(username=username)
        #邮箱状态更新
        user.email_active = True
        user.save()

        ser = UserDetailSerializer(user)

        return Response(ser.data)

class AreasView(CacheResponseMixin,ListAPIView):
    serializer_class = AreaSerializer
    # queryset = None
    #制定查询机,获取省的信息
    def get(self, request, pk=None,*args, **kwargs):

        if pk == '':
            AreasView.queryset = Area.objects.filter(parent=None)
        else:
            pk = int(pk)
            AreasView.queryset = Area.objects.filter(parent=pk)
        return self.list(request,*args, **kwargs)

class AddressesView(CreateAPIView,UpdateAPIView,ListAPIView):

    serializer_class = AddressSerializer
    queryset = Address.objects.all()

    def delete(self,request,pk):

        obj=Address.objects.get(id = pk)
        obj.is_deleted = True
        obj.save()

        return Response({'message':'ok'})

    def get_queryset(self):

        return Address.objects.filter(user=self.request.user)




class DefautView(APIView):

    def put(self,request,pk):
        return Response({'message':'ok'})




